package com.itheima.project.service.impl;

import com.itheima.project.pojo.ContentBaseRule;
import com.itheima.project.mapper.ContentBaseRuleMapper;
import com.itheima.project.service.IContentBaseRuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.itheima.project.vo.content.ContentBaseRuleVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.project.utils.BeanConv;
import com.itheima.project.utils.EmptyUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * @Description：服务实现类
 */
@Service
public class ContentBaseRuleServiceImpl extends ServiceImpl<ContentBaseRuleMapper, ContentBaseRule> implements IContentBaseRuleService {

    @Override
    public Page<ContentBaseRule> findContentBaseRulePage(ContentBaseRuleVo contentBaseRuleVo, int pageNum, int pageSize) {
        //构建分页对象
        Page<ContentBaseRule> page = new Page<>(pageNum,pageSize);
        //构建查询条件
        QueryWrapper<ContentBaseRule> queryWrapper = new QueryWrapper<>();
        //构建多条件查询，代码生成后自己可自行调整
        //审核规则所属方：ALI、BAIDU、TENCENT查询
        if (!EmptyUtil.isNullOrEmpty(contentBaseRuleVo.getPlaformLabel())) {
            queryWrapper.lambda().eq(ContentBaseRule::getPlaformLabel,contentBaseRuleVo.getPlaformLabel());
        }
        //业务类型：视频、音频 图片等查询
        if (!EmptyUtil.isNullOrEmpty(contentBaseRuleVo.getBusinessType())) {
            queryWrapper.lambda().eq(ContentBaseRule::getBusinessType,contentBaseRuleVo.getBusinessType());
        }
        //审核等级查询
        if (!EmptyUtil.isNullOrEmpty(contentBaseRuleVo.getLevel())) {
            queryWrapper.lambda().eq(ContentBaseRule::getLevel,contentBaseRuleVo.getLevel());
        }
        //达标分数查询
        if (!EmptyUtil.isNullOrEmpty(contentBaseRuleVo.getValidScore())) {
            queryWrapper.lambda().eq(ContentBaseRule::getValidScore,contentBaseRuleVo.getValidScore());
        }
        //备注说明查询
        if (!EmptyUtil.isNullOrEmpty(contentBaseRuleVo.getRemark())) {
            queryWrapper.lambda().eq(ContentBaseRule::getRemark,contentBaseRuleVo.getRemark());
        }
        //状态查询
        if (!EmptyUtil.isNullOrEmpty(contentBaseRuleVo.getEnableFlag())) {
            queryWrapper.lambda().eq(ContentBaseRule::getEnableFlag,contentBaseRuleVo.getEnableFlag());
        }
        //按创建时间降序
        queryWrapper.lambda().orderByDesc(ContentBaseRule::getCreatedTime);
        //执行分页查询
        return page(page, queryWrapper);
    }

    @Override
    public ContentBaseRule createContentBaseRule(ContentBaseRuleVo contentBaseRuleVo) {
        //转换ContentBaseRuleVo为ContentBaseRule
        ContentBaseRule contentBaseRule = BeanConv.toBean(contentBaseRuleVo, ContentBaseRule.class);
        boolean flag = save(contentBaseRule);
        if (flag){
        return contentBaseRule;
        }
        return null;
    }

    @Override
    public Boolean updateContentBaseRule(ContentBaseRuleVo contentBaseRuleVo) {
        //转换ContentBaseRuleVo为ContentBaseRule
        ContentBaseRule contentBaseRule = BeanConv.toBean(contentBaseRuleVo, ContentBaseRule.class);
        return updateById(contentBaseRule);
    }

    @Override
    public Boolean deleteContentBaseRule(String[] checkedIds) {
        //转换数组为集合
        List<String> ids = Arrays.asList(checkedIds);
        List<Long> idsLong = new ArrayList<>();
        ids.forEach(n->{
            idsLong.add(Long.valueOf(n));
        });
        return removeByIds(idsLong);
    }

    @Override
    public List<ContentBaseRule> findContentBaseRuleList(ContentBaseRuleVo contentBaseRuleVo) {
        //构建查询条件
        QueryWrapper<ContentBaseRule> queryWrapper = new QueryWrapper<>();
        if (!EmptyUtil.isNullOrEmpty(contentBaseRuleVo.getId())) {
            queryWrapper.lambda().eq(ContentBaseRule::getId,contentBaseRuleVo.getId());
        }
        //审核规则所属方：ALI、BAIDU、TENCENT查询
        if (!EmptyUtil.isNullOrEmpty(contentBaseRuleVo.getPlaformLabel())) {
            queryWrapper.lambda().eq(ContentBaseRule::getPlaformLabel,contentBaseRuleVo.getPlaformLabel());
        }
        //业务类型：视频、音频 图片等查询
        if (!EmptyUtil.isNullOrEmpty(contentBaseRuleVo.getBusinessType())) {
            queryWrapper.lambda().eq(ContentBaseRule::getBusinessType,contentBaseRuleVo.getBusinessType());
        }
        //审核等级查询
        if (!EmptyUtil.isNullOrEmpty(contentBaseRuleVo.getLevel())) {
            queryWrapper.lambda().eq(ContentBaseRule::getLevel,contentBaseRuleVo.getLevel());
        }
        //达标分数查询
        if (!EmptyUtil.isNullOrEmpty(contentBaseRuleVo.getValidScore())) {
            queryWrapper.lambda().eq(ContentBaseRule::getValidScore,contentBaseRuleVo.getValidScore());
        }
        //备注说明查询
        if (!EmptyUtil.isNullOrEmpty(contentBaseRuleVo.getRemark())) {
            queryWrapper.lambda().eq(ContentBaseRule::getRemark,contentBaseRuleVo.getRemark());
        }
        //状态查询
        if (!EmptyUtil.isNullOrEmpty(contentBaseRuleVo.getEnableFlag())) {
            queryWrapper.lambda().eq(ContentBaseRule::getEnableFlag,contentBaseRuleVo.getEnableFlag());
        }
        return list(queryWrapper);
    }
}
